package com.tusuo.stds.ui.scenedetail;

import android.content.Context;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.SceneDetail;
import com.tusuo.stds.rest.SceneList;

import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-4-20.
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(SceneDetailPresenter.class)
public class SceneDetailPresenter extends MvpBasePresenter<SceneDetailView> {

    @Inject
    ApiHelper mApiHelper;

    Context mContext;

    public SceneDetailPresenter(Context context) {
        this.mContext = context;
        Injector.initSceneDetailPresenterComponent().inject(this);
    }

    public void initData(int sceneId) {
        mApiHelper.getSceneDetail(new GetSceneDetailSubscriber(),sceneId);
    }


    private class GetDependSceneListSubscriber extends Subscriber<SceneList> {
        int sceneId;

        public GetDependSceneListSubscriber(int sceneId) {
            this.sceneId = sceneId;
        }


        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(SceneList sceneList) {
            if (sceneList != null) {
                List<SceneList.Scene> data = sceneList.Data;
                getView().initDependScene(data,sceneId);
            }
        }

    }

    private class GetSceneDetailSubscriber extends Subscriber<SceneDetail> {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(SceneDetail sceneDetail) {
            if (sceneDetail != null) {
                getView().initAllView(sceneDetail);
                mApiHelper.getSceneList(new GetDependSceneListSubscriber(sceneDetail.DependId));
            }
        }
    }
}
